<template>
    <div class="goodsInfo-container">

        <transition @befor-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
            <div class="ball" v-if="ballFlag" ref="ball"></div>
        </transition>

        <!-- 商品轮播 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <slide :swipeList="slidePhoto" :isFull="false"></slide>
                </div>
            </div>
        </div>
        <!-- 商品购买区域 -->
        <div class="mui-card">
            <div class="mui-card-header">{{goodsInfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价: <del>￥{{goodsInfo.market_price}}</del>  销售价:<span class="now-price">￥{{goodsInfo.sell_price}}</span>
                    </p>
                    <p>购买数量:</p>
                    <div class="mui-numbox" data-numbox-min='1'>
                        <button class="mui-btn mui-btn-numbox-minus" type="button">-</button>
                        <input id="test" class="mui-input-numbox" type="number" value="1" ref="num" />
                        <button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
                    </div>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addShop">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>
        <!-- 商品参数区域 -->
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>编号:{{goodsInfo.goods_no}}</p>
                    <p>剩余<span class="important">{{goodsInfo.stock_quantity}}</span>件</p>
                    <p>上架时间:{{goodsInfo.add_time | dateFormat}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" @click="goDetail(id)" size="large" plain>图文介绍</mt-button>
                <mt-button type="danger" @click="goComment(id)" size="large" plain>商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
    import Slide from "../subcom/Slide.vue";
    export default {
        name: "GoodsInfo",
        mounted() {
            mui('.mui-numbox').numbox();
        },
        data() {
            return {
                id: this.$route.params.id, //将参数id挂载于data中
                slidePhoto: [],
                goodsInfo: {},
                ballFlag: false,
                selectedCount: 1
            }
        },
        created() {
            this.getSlidePhote();
            this.getGoodsInfo();
        },
        methods: {
            getSlidePhote() {
                this.axios.get(`getthumimages/${this.id}`)
                    .then(response => {
                        if (response.data.status === 0) {
                            response.data.message.forEach(item => {
                                item.img = item.src;
                            });
                            this.slidePhoto = response.data.message;
                        } else {
                            this.$toast('获取商品列表失败!');
                        }
                    })
            },
            getGoodsInfo() {
                this.axios.get(`goods/getinfo/${this.id}`)
                    .then(response => {
                        if (response.data.status === 0) {
                            this.goodsInfo = response.data.message[0];
                        } else {
                            this.$toast('获取商品信息失败!');
                        }
                    });
            },
            goDetail(id) {
                this.$router.push({name: 'goodsDetail', params: {id}});
            },
            goComment(id) {
                this.$router.push({name: 'goodsComment', params: {id}});
            },
            addShop() {
                this.ballFlag = !this.ballFlag;
                this.selectedCount = this.$refs.num.value;
                let goods = {id: this.id, count: this.selectedCount, price: this.goodsInfo.sell_price, selected: true, maxCount: this.goodsInfo.stock_quantity};
                console.log(goods);
                // let o = {id: this.id, count: this.goodsInfo.stock_quantity};
                this.$store.commit('addToCar', goods);
                // this.$store.commit('addMaxForId', o);
            },
            beforeEnter(el) {
                el.style.transform =  `translate(0, 0)`;
            },
            enter(el, done) {
                el.offsetWidth;
                //获取小球在页面中的位置
                let ballPosition = this.$refs.ball.getBoundingClientRect();
                //获取徽标在页面中的位置
                let badgePosition = document.getElementById('badge').getBoundingClientRect();
                let x = badgePosition.left - ballPosition.left;
                let y = badgePosition.top - ballPosition.top;
                el.style.transform = `translate(${x}px, ${y}px)`;
                el.style.transition = 'all 1.5s cubic-bezier(.08,.82,.17,1)';
                done()
            },
            afterEnter(el) {
                this.ballFlag = !this.ballFlag;
            },
        },
        components: {
            slide: Slide,
        },
        watch: {
            'goodsInfo': function (newVal, oldVal) {
                mui(".mui-numbox").numbox().setOption('max',newVal.stock_quantity)
            }
        }
    }
</script>

<style scoped>
    .goodsInfo-container {
        background-color: #eee;
        overflow: hidden;
    }
    .mui-card-header {
        font-size: 40px;
        line-height: 40px;
    }
    .mui-card-content-inner p {
        font-size: 32px;
        line-height: 32px;
    }
    .mint-button--small {
        font-size: 32px;
        height: 80px;
        float: right;
        margin: 10px;
    }
    .mint-button--large {
        height: 80px;
        font-size: 30px;
        margin: 20px 0;
    }
    .now-price {
        font-size: 38px;
        color: red;
    }
    .mui-numbox {
        width: 205px;
        height: 60px;
        line-height: 60px;
    }
    .mui-card-footer, .mui-card-header {
        display: block;
    }
    .mui-numbox .mui-input-numbox, .mui-numbox .mui-numbox-input{
        font-size: 35px;
    }
    .mui-numbox [class*=btn-numbox], .mui-numbox [class*=numbox-btn] {
        font-size: 50px;
    }
    .important {
        color: red;
    }
    .ball {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background-color: red;
        position: absolute;
        z-index: 99;
        top: 767px;
        left: 116px;
    }
</style>